﻿using System;
using System.Diagnostics;
using System.Linq;
using JetBlack.FeedBus.Distributor;
using JetBlack.Services;

namespace FeedBusDistributorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagedServer<Server> managedServer = new ManagedServer<Server>(new Server());
            string helptext = "Enter command: start /port:9123, stop, pause, resume, verbose, info, warning, error, off.";

            if (args.Length > 0)
            {
                System.Console.WriteLine("Starting server with command line arguments");
                managedServer.Start(args);
            }

            try
            {
                System.Console.WriteLine(helptext);
                while (true)
                {
                    string line = System.Console.ReadLine();
                    string[] parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 0)
                        continue;

                    string cmd = parts[0];
                    args = parts.Skip(1).ToArray();

                    switch (cmd)
                    {
                        case "start":
                            {
                                if (args.Length == 0)
                                    args = new[] { "/port:9123" };
                                managedServer.Start(args);
                            }
                            break;
                        case "stop":
                            managedServer.Stop();
                            break;
                        case "pause":
                            managedServer.Pause();
                            break;
                        case "resume":
                            managedServer.Resume();
                            break;
                        case "verbose":
                            managedServer.Server.TraceSource.Switch.Level = SourceLevels.Verbose;
                            break;
                        case "info":
                            managedServer.Server.TraceSource.Switch.Level = SourceLevels.Information;
                            break;
                        case "warning":
                            managedServer.Server.TraceSource.Switch.Level = SourceLevels.Warning;
                            break;
                        case "error":
                            managedServer.Server.TraceSource.Switch.Level = SourceLevels.Error;
                            break;
                        case "off":
                            managedServer.Server.TraceSource.Switch.Level = SourceLevels.Off;
                            break;
                        default:
                            System.Console.WriteLine("error: unknown command \"{0}\"", line);
                            System.Console.WriteLine(helptext);
                            break;
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.ToString());
            }
        }
    }
}
